<template lang="">
  <!-- 查询 -->
  <el-form :inline="true" :model="formInline" class="demo-form-inline">
    <el-form-item label="库位编码">
      <el-input
        v-model="formInline.locationCode"
        placeholder="库位编码"
        clearable
      />
    </el-form-item>
    <el-form-item label="库位名称">
      <el-input
        v-model="formInline.locationName"
        placeholder="库位名称"
        clearable
      />
    </el-form-item>
    <el-form-item label="仓库">
      <el-select v-model="formInline.warehouseId" placeholder="仓库" clearable>
        <el-option
          v-for="item in typeData"
          :key="item.warehouseId"
          :label="item.warehouseName"
          :value="item.warehouseId"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="loadData()">查询</el-button>
      <el-button type="success" @click="openDialog()">新增</el-button>
    </el-form-item>
  </el-form>
  <!-- 显示列表 -->
  <el-table :data="tableData" border style="width: 100%">
    <el-table-column type="selection" width="55" />
    <el-table-column prop="locationCode" label="库位编码" width="180" />
    <el-table-column prop="locationName" label="库位名称" width="180" />

    <el-table-column prop="locationTypeName" label="库位类型" width="180" />
    <el-table-column prop="warehouseName" label="仓库" width="180" />
    <el-table-column prop="isDisable" label="是否禁用" width="180">
      <template #default="scope">
        <el-tag type="primary" v-if="scope.row.isDisable == true">{{
          scope.row.isDisable
        }}</el-tag>
        <el-tag type="danger" v-else>{{ scope.row.isDisable }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="isDefault" label="是否默认" width="180">
      <template #default="scope">
        <el-tag type="primary" v-if="scope.row.isDefault == true">{{
          scope.row.isDefault
        }}</el-tag>
        <el-tag type="danger" v-else>{{ scope.row.isDefault }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="remark" label="备注" width="180" />
    <el-table-column
      prop="createTime"
      label="创建时间"
      width="180"
      :formatter="formatter"
    />
    <el-table-column label="操作" width="180">
      <template #default="scope">
        <el-button
          type="danger"
          size="small"
          @click="handleDelete(scope.row.locationId)"
          >删除</el-button
        >
        <el-button
          type="success"
          size="small"
          @click="openDialog(scope.row.locationId)"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="page">
    <el-pagination
      v-model:current-page="formInline.pageindex"
      v-model:page-size="formInline.pagesize"
      :page-sizes="[1, 2, 10, 12, 14]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      prev-text="上一页"
      next-text="下一页"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <!-- 弹框 -->
  <el-dialog
    v-model="dialog.visible"
    :title="dialog.title"
    @closed="closeDialog"
    width="650px"
  >
    <el-form ref="LocationFormRef" :model="form" label-width="120px">
      <el-form-item label="库位编码">
        <el-input v-model="form.locationCode" placeholder="请输入库位编码" />
      </el-form-item>
      <el-form-item label="库位名称">
        <el-input v-model="form.locationName" placeholder="请输入库位名称" />
      </el-form-item>
      <el-form-item label="仓库">
        <el-select
          v-model="form.warehouseId"
          placeholder="请输入仓库"
          clearable
        >
          <el-option
            v-for="item in typeData"
            :key="item.warehouseId"
            :label="item.warehouseName"
            :value="item.warehouseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库位类型">
        <el-select
          v-model="form.locationTypeId"
          placeholder="请输入库位类型"
          clearable
        >
          <el-option
            v-for="item in locationTypeData"
            :key="item.locationTypeId"
            :label="item.locationTypeName"
            :value="item.locationTypeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="面积">
        <el-input v-model="form.locationArea" placeholder="请输入面积" />
      </el-form-item>
      <!-- <el-form-item label="是否禁用">
        <el-switch
          v-model="form.isDisable"
          style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
          active-value="100"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="是否默认">
        <el-switch
          v-model="form.isDefault"
          style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
          active-value="100"
          inactive-value="0"
        />
      </el-form-item> -->
      <el-form-item label="备注">
        <el-input v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit()"> 确 定 </el-button>
        <el-button @click="closeDialog"> 取 消 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import {
  getLocation,
  getWarehouse,
  getLocationType,
  AddLocation,
  DelLocation,
  BackFillLocation,
  UpdLocation,
} from "@/api/location/index";
import type { TableColumnCtx } from "element-plus";
import moment from "moment";

const tableData = ref([]);
const totalCount = ref(0);
const typeData = ref([]);
const locationTypeData = ref([]);

const formInline = reactive({
  locationCode: "",
  locationName: "",
  warehouseId: "",
  pageindex: 1,
  pagesize: 10,
});

// moment格式化时间
const formatter = (row: any, column: TableColumnCtx<any>) => {
  return moment(row.leaseTime).format("YYYY-MM-DD");
};

// 仓库下拉列表显示
const loadType = async () => {
  var list = await getWarehouse();
  typeData.value = list.data.rows;
};

// 库位类型下拉列表显示
const locationType = async () => {
  var list = await getLocationType();
  locationTypeData.value = list.data;
};

// 分页查询显示库位
const loadData = async () => {
  var list = await getLocation(formInline);
  tableData.value = list.data.rows;
  totalCount.value = list.data.totalRows;
};

// 改变页索引
const handleCurrentChange = async (val: number) => {
  formInline.pageindex = val;
  await loadData();
};

// 改变页大小
const handleSizeChange = async (val: number) => {
  formInline.pagesize = val;
  await loadData();
};

//删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      DelLocation(row).then((ser: any) => {
        ElMessage.success(ser.message);
        loadData();
      });
    })
    .catch(() => {
      //ElMessage.error("删除操作出错，请稍后重试");
      ElMessage.info("已取消删除");
    });
};

// 弹框
const dialog = reactive({
  title: "",
  visible: false,
});
const form = ref({
  locationCode: undefined,
  locationName: undefined,
  warehouseId: undefined,
  locationTypeId: undefined,
  locationArea: undefined,
  // isDisable: undefined,
  // isDefault: undefined,
  remark: undefined,
});

var kws = ref();

// 打开仓库表单弹窗
async function openDialog(locationId?: string) {
  dialog.visible = true;
  if (locationId) {
    dialog.title = "修改库位";
    BackFillLocation(locationId).then((ser) => {
      form.value = ser.data;
      kws.value = locationId;
    });
  } else {
    dialog.title = "新增库位";
  }
}

const LocationFormRef = ref(Element);

// 仓库表单提交
async function handleSubmit(Id?: number) {
  console.log(Id);

  Id = kws.value;
  if (Id) {
    UpdLocation(form.value).then(async () => {
      ElMessage.success("修改成功");
      closeDialog();
      await loadData();
    });

    closeDialog();
    await loadData();
  } else {
    ElMessage.success("新增成功");
    AddLocation(form.value).then(async (ser: any) => {
      closeDialog();
      await loadData();
    });
  }
}

/** 关闭弹窗 */
function closeDialog() {
  dialog.visible = false;
  //resetForm();
}

// 回调函数
onMounted(async () => {
  await loadData();
  await loadType();
  await locationType();
});
</script>
<style>
.page {
  display: flex;
  justify-content: center;
  margin-top: 16px;
}
.demo-form-inline .el-input {
  --el-input-width: 160px;
}

.demo-form-inline .el-select {
  --el-select-width: 160px;
}
</style>
